package database.demo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class PassengersOperations {

	// Database fields
	private DataBaseWrapperPassengers dbHelper;
	private String[] PASSENGER_TABLE_COLUMNS = {
			DataBaseWrapperPassengers.PASSENGER_ID,
			DataBaseWrapperPassengers.PASSENGER_LASTNAME,
			DataBaseWrapperPassengers.PASSENGER_FIRSTNAME,
			DataBaseWrapperPassengers.PASSENGER_PHONE,
			DataBaseWrapperPassengers.PASSENGER_EMAIL,
			DataBaseWrapperPassengers.PASSENGER_NOTE,
			DataBaseWrapperPassengers.PASSENGER_PRESENT,
			DataBaseWrapperPassengers.PASSENGER_UPDATE };
	private String[] PASSENGER_TABLE_ID = { DataBaseWrapperPassengers.PASSENGER_ID };
	private SQLiteDatabase database;

	public PassengersOperations(Context context) {
		dbHelper = new DataBaseWrapperPassengers(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public Passenger addPassenger(String ID, String lastname, String firstname,
			String phone, String email) {
		Passenger newComment = null;
		ContentValues values = new ContentValues();
		values.put(DataBaseWrapperPassengers.PASSENGER_ID, ID);
		values.put(DataBaseWrapperPassengers.PASSENGER_LASTNAME, lastname);
		values.put(DataBaseWrapperPassengers.PASSENGER_FIRSTNAME, firstname);
		values.put(DataBaseWrapperPassengers.PASSENGER_PHONE, phone);
		values.put(DataBaseWrapperPassengers.PASSENGER_EMAIL, email);
		values.put(DataBaseWrapperPassengers.PASSENGER_NOTE, "");
		values.put(DataBaseWrapperPassengers.PASSENGER_PRESENT, false);
		values.put(DataBaseWrapperPassengers.PASSENGER_UPDATE, "");

		String where = "_id like ?";
		String[] whereArgs = new String[] { ID };
		if (database.query(true, DataBaseWrapperPassengers.PASSENGERS,
				PASSENGER_TABLE_COLUMNS, where, whereArgs, null, null, null,
				null).getCount() < 1) {

			database.insert(DataBaseWrapperPassengers.PASSENGERS, null, values);

			// now that the passenger is created return it ...
			Cursor cursor = database.query(
					DataBaseWrapperPassengers.PASSENGERS,
					PASSENGER_TABLE_COLUMNS,
					DataBaseWrapperPassengers.PASSENGER_ID + " = " + ID, null,
					null, null, null);
			cursor.moveToFirst();
			newComment = parsePassenger(cursor);
			cursor.close();
		}
		return newComment;
	}

	public void updatePassengerDetails(String tmpid, String tmpLast,
			String tmpFirst, String tmpPhone, String tmpMail) {
		ContentValues values = new ContentValues();
		values.put(DataBaseWrapperPassengers.PASSENGER_ID, tmpid);
		values.put(DataBaseWrapperPassengers.PASSENGER_LASTNAME, tmpLast);
		values.put(DataBaseWrapperPassengers.PASSENGER_FIRSTNAME, tmpFirst);
		values.put(DataBaseWrapperPassengers.PASSENGER_PHONE, tmpPhone);
		values.put(DataBaseWrapperPassengers.PASSENGER_EMAIL, tmpMail);
		String where = "_id like ?";
		String[] whereArgs = new String[] { String.valueOf(tmpid) };
		database.update(DataBaseWrapperPassengers.PASSENGERS, values, where,
				whereArgs);
	}

	public void updatePassengerPresene(String tmpid, String tmpNote,
			boolean tmpPresence, String tmpUpdate) {
		ContentValues values = new ContentValues();
		values.put(DataBaseWrapperPassengers.PASSENGER_ID, tmpid);
		values.put(DataBaseWrapperPassengers.PASSENGER_NOTE, tmpNote);
		values.put(DataBaseWrapperPassengers.PASSENGER_PRESENT, tmpPresence);
		values.put(DataBaseWrapperPassengers.PASSENGER_UPDATE, tmpUpdate);
		String where = "_id like ?";
		String[] whereArgs = new String[] { String.valueOf(tmpid) };
		database.update(DataBaseWrapperPassengers.PASSENGERS, values, where,
				whereArgs);
	}

	public boolean deletePassenger(String passID) {
		if (database.delete(DataBaseWrapperPassengers.PASSENGERS,
				DataBaseWrapperPassengers.PASSENGER_ID + " = " + passID, null) > 0) {
			System.out.println("Passenger deleted with id: " + passID);
			return true;
		} else
			return false;
	}

	public Cursor getAllPassengers() {
		Cursor cursor = database.query(DataBaseWrapperPassengers.PASSENGERS,
				PASSENGER_TABLE_COLUMNS, null, null, null, null,
				DataBaseWrapperPassengers.PASSENGER_LASTNAME);
		if (cursor != null) {
			cursor.moveToFirst();
		}
		return cursor;
	}

	// Get a specific row (by tmpId)
	public Cursor getPass(String tmpId) {
		String where = DataBaseWrapperPassengers.PASSENGER_ID + " like "
				+ tmpId;
		Cursor c = database.query(true, DataBaseWrapperPassengers.PASSENGERS,
				PASSENGER_TABLE_COLUMNS, where, null, null, null, null, null);
		if (c != null) {
			c.moveToFirst();
		}
		return c;
	}

	// Get a specific row (by tmpId)
	public Cursor getUpdate() {
		Cursor c = database.query(true, DataBaseWrapperPassengers.PASSENGERS,
				PASSENGER_TABLE_COLUMNS, null, null, null, null, null, "1");
		if (c != null) {
			c.moveToFirst();
		}
		return c;
	}

	public Cursor getMaxID() {
			Cursor c = 	database.query(DataBaseWrapperPassengers.PASSENGERS, null, "_id=(SELECT MAX(_id))", null, null, null, null);
		if (c != null) {
			c.moveToLast();
		}
		return c;
	}

	private Passenger parsePassenger(Cursor cursor) {
		Passenger passenger = new Passenger();
		passenger.setID((cursor.getString(0)));
		passenger.setLastName(cursor.getString(1));
		passenger.setFirstName(cursor.getString(2));

		return passenger;
	}

}